package com.example.oauth.tracing;

import io.opentracing.propagation.TextMap;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class HttpHeadersInjectAdapter implements TextMap {

    private Map<String, Collection<String>> headers;

    public HttpHeadersInjectAdapter(Map<String, Collection<String>> headers) {
        if (headers == null) {
            throw new NullPointerException("Headers should not be null!");
        }

        this.headers = headers;
    }

    @Override
    public void put(String key, String value) {
        Collection<String> values = headers.get(key);
        if (values == null) {
            values = new ArrayList<>(1);
            headers.put(key, values);
        }

        values.add(value);
    }

    @Override
    public Iterator<Map.Entry<String, String>> iterator() {
        throw new UnsupportedOperationException("This class should be used only with tracer#inject()");
    }
}
